import axios from "axios"

export default {
    namespaced: true,
    state() {
        return {
            list: []
        }
    },
    mutations: {
        GETCARTLIST(state, value) {
            state.list = value
        }
    },
    actions: {
        async getCartList(context) {
            await axios.get('/api/vuex/list').then(res => {
                context.commit('GETCARTLIST', res.data.cart)
            })
        },
    },
    getters: {
        total(state){
            return state.list.reduce((sum,item)=> sum + item.count, 0)
        },
        totalPrice(state){
            return state.list.reduce((sum,item) => sum + item.count * item.price, 0)
            // return state.list.reduce((sum,item) => {
            //     return sum + item.count * item.price
            // },0)
            
        }
    }
}